(function( $ ){

   function formInit() {
        var clsInput = 'input-xlarge',
            clsEmpty = 'input-xlarge',
            clsDisabled = 'form-item-disabled',
            clsError = 'error',
            clsPassword = 'form-item-pwd';

        $( '.form-item input' ).each(function(){
            var me = this,
                $me = $( me ),
                parent = $( me.parentNode ),
                label = parent.find( '.k' ),
                offsetX = $me.position().left;
            if ( !( this.type == 'text' || this.type == 'password' ) ) {
                return;
            }
            // some input do not need init
            if ($me.hasClass('no-init')) {
                return;
            }
            if ( me.value !=='' ) {
                me.className = clsInput;
            } else {
                me.className = clsEmpty;
                if ( offsetX > 0 ) {
                    label.css({ 'left': offsetX + 10 });
                }
            }
            if ( me.disabled ) {
                me.addClass( clsDisabled );
            }
            

            label.on( 'click', function(e){
                try {
                    me.focus();
                } catch ( ex ) {}
            });

        });

        function addInputEvent() {
            $( '.input-xlarge' )
                .on( 'focus', function(){
                    
					
                    var me = $( this ),
                        parent = $( this.parentNode ),
                        label = parent.find( '.k' ),
                        t = parent.find( '.text-error' ),
                        classname;
                    if (me.hasClass('no-init')) {
                        return;
                    }
                    if ( parent.hasClass( clsPassword ) ) {
                        classname = clsInput + ' ' + clsPassword;
                    } else {
                        classname = clsInput;
                    }
                    if ( me.hasClass( clsError ) ) {
                        t.hide();
						me.removeClass(clsError);
                    }
                    label.css({ 'left': 'auto' });
                    label.hide();
                    me.className = classname;
                })
                .on( 'blur', function(){
                    var me = this,
                        $me = $( me ),
                        parent = $( me.parentNode ),
                        classname,
                        label = parent.find( '.k' ),
                        offsetX = $me.position().left;
                 
                    if ($(this).hasClass('no-init')) {
                        return;
                    }
                    label.show();
                    if ( me.value == '' ) {
                        if ( parent.hasClass( clsPassword ) ) {
                            classname = clsEmpty + ' ' + clsPassword;
                        } else {
                            classname = clsEmpty;
                        }
                        if ( offsetX > 0 ) {
                            label.css({ 'left': offsetX + 10 });
                        } else {
                            label.css({ 'left': 12 });
                        }
                        me.className = classname;
                    }
                });
        };

        function delInputEvent() {
            $( '.form-item input' ).off( 'focus' );
            $( '.form-item input' ).off( 'blur' );
        };
       
        addInputEvent();
    }
	
	
	
    $.formInit = formInit;
	

})(jQuery);